//
//  VideoPlayerController.m
//  OurLottery
//
//  Created by hubing on 2018/6/1.
//  Copyright © 2018年 hl. All rights reserved.
//

#import "VideoPlayerController.h"
#import "YGPlayerView.h"
#import "YGPlayInfo.h"

@interface VideoPlayerController ()<UIWebViewDelegate>
@property (nonatomic, strong) NSDictionary *dataDic;
@property (nonatomic, strong) YGPlayInfo *playInfo;
@property (nonatomic,strong) YGPlayerView *player;

@end

@implementation VideoPlayerController

-(instancetype)initWithData:(NSDictionary *)dataDic{
    if (self = [super init]) {
        _dataDic = dataDic;
    }
    return self;
}
 
- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = RGBOF(0xf1f1f1);
    [self setupPlayerView];
}

-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    AppDelegate *appdelegate =  (AppDelegate *)[[UIApplication sharedApplication] delegate];
    appdelegate.allowRotation = YES;
    
    [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(back:) name:@"backToVideoList" object:nil];
}
-(void)viewWillDisappear:(BOOL)animated{
    [super viewWillDisappear:animated];
    [[NSNotificationCenter defaultCenter]removeObserver:self name:@"backToVideoList" object:nil];
}

-(void)back:(NSNotification *)notify{
    [self dismissViewControllerAnimated:NO completion:nil];
}

- (void)setupPlayerView
{
    if (_player) {
        [_player removeFromSuperview];
    }
    _player = [[[NSBundle mainBundle] loadNibNamed:@"YGPlayerView" owner:nil options:nil] lastObject];
    [self.view addSubview:_player];
    YGPlayInfo *playInfo = [[YGPlayInfo alloc]init];
    playInfo.name = _dataDic[@"name"];
    playInfo.url = _dataDic[@"url"];
    playInfo.cover_url = _dataDic[@"cover_url"];
    [_player playWithPlayInfo:playInfo];
}

@end
